﻿using System;
using System.Collections.Generic;
using TSites.GUI.Enums;
using System.IO;
using TSites.Offices;
using TSites.Service.Entities;
using TSites.GUI.Entities;

namespace TSites.GUI
{
	public abstract class BasedContractExporter
	{
		protected string _templateFolder;
		protected string _exportFolder;
		protected string _fileName;

		public BasedContractExporter(string templateFolder, string exportFolder, string fileName)
		{
			_templateFolder = templateFolder;
			_exportFolder = exportFolder;
			_fileName = fileName;
		}

		public void ExportAndPrint(LANGUAGE language, string printerName = null)
		{
			string templatePath, filePath;
			Dictionary<string, object> bookmarkHash = new Dictionary<string, object>();
			// build Immanuel bookmark
			ConfigurationSingleton.Instance.Immanuel.AddBookMarks(bookmarkHash, language);
			// get Client bookmark
			GetTemplateBookmark(bookmarkHash, language);

			// build the paths
			templatePath = Path.Combine(_templateFolder, GetContractName() + "_" + language.ToString() + ".doc");
			filePath = Path.Combine(_exportFolder, GetContractName(), _fileName + "_" + language.ToString() + ".doc");
			if (File.Exists(filePath)) // file is already existed
			{
				filePath = Path.Combine(_exportFolder, GetContractName(),
					_fileName + "_" + (DateTime.Now.ToString("yyMMddHHmmss")) + "_" + language.ToString() + ".doc");
			}
			// export
			bool isvisible = printerName == null;
			WordObject.ExportAndPrint(templatePath, bookmarkHash, filePath, isvisible, printerName);
		}

		protected abstract string GetContractName();
		protected abstract void GetTemplateBookmark(Dictionary<string, object> templateHash, LANGUAGE language);
	}

	public class AuditContractExpoter : BasedContractExporter
	{
		public AuditContractExpoter(string templatePath, string exportFolder, string fileName)
			: base(templatePath, exportFolder, fileName)
		{ }

		public AuditContractExpoter(string templatePath, string exportFolder, string fileName, Company client, AuditContractParam auditParams)
			: base(templatePath, exportFolder, fileName)
		{
			_client = client;
			_auditParams = auditParams;
		}

		private Company _client;
		private AuditContractParam _auditParams;

		protected override string GetContractName()
		{
			return "AuditContract";
		}

		protected override void GetTemplateBookmark(Dictionary<string, object> templateHash, LANGUAGE language)
		{
			_client.AddBookMarks(templateHash, (int)language);
			_auditParams.AddBookmarks(templateHash, language);
		}
	}
}
